<?php

namespace app\api\controller;
use think\Db;
use app\common\model\Address as AddressModel;
use think\Loader;
use app\common\controller\Api;

class Address extends Api
{

    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 地址列表
     */
    public function list()
    {
        $uid = $this->auth->id;
        $list = AddressModel::all(['uid' => $uid]);
        $this->success('请求成功',$list);
    }

    /**
     * 添加地址
     * username 用户名
     * sex 0 女 1男
     * phone 手机号
     * address 服务地址
     * detail 详细地址
     * lon 经度
     * lat 纬度
     * default 是否默认:1 默认地址
     */

    public function add_address(){
        $uid = $this->auth->id;
        $address  = $this->request->request("address");
        $username = $this->request->request("username");
        $mobile   = $this->request->request("mobile");
        $sex      = $this->request->request("sex",0);
        $detail   = $this->request->request("detail");
        $default  = $this->request->request("default");
        $lon      = $this->request->request("lon",0);
        $lat      = $this->request->request("lat",0);


        if($lon=='null'||$lat=='null'||$lon=='undefined'||$lat=='undefined'){
            $this->error('坐标信息错误');
        }
        if ($default == 1) {
            AddressModel::update(['is_default' => 0],['uid' => $uid]);
        }
        if ($sex != 1) {
            $sex = 0;
        }
        $data     = [
            'uid'        => $uid,
            'address'    => $address,
            'username'   => $username,
            'mobile'     => $mobile,
            'is_default' => $default,
            'detail'     => $detail,
            'sex'        => $sex,
            'lon'        => $lon,
            'lat'        => $lat
        ];
//        if (!preg_match("/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/",$mobile)) {
//            $this->error('手机号格式错误!');
//        }
        $result = AddressModel::create($data);
        if ($result) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 编辑用户地址
     *
     * @param string $address  地址
     * @param string $username 用户姓名
     * @param string $mobile   电话
     */
    public function edit_address()
    {
        $uid = $this->auth->id;

        $address_id = $this->request->request('address_id');
        $address    = $this->request->request("address");
        $username   = $this->request->request("username");
        $mobile     = $this->request->request("mobile");
        $sex        = $this->request->request("sex");
        $detail     = $this->request->request("detail");
        $default    = $this->request->request("default");
        $lon      = $this->request->request("lon",0);
        $lat      = $this->request->request("lat",0);
        if($lon=='null'||$lat=='null'||$lon=='undefined'||$lat=='undefined'){
            $this->error('坐标信息错误');
        }

        $address_info = AddressModel::get(['id' => $address_id,'uid' => $uid]);
        if (!$address_info) {
            $this->error('未找到此地址信息');
        }

        if ($address) {
            $address_info->address = $address;
        }
        if ($username) {
            $address_info->username = $username;
        }
        if ($mobile) {
//            if (!preg_match("/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/",$mobile)) {
//                $this->error('手机号格式错误!');
//            }
            $address_info->mobile = $mobile;
        }
        if ($sex) {
            $address_info->sex = $sex;
        }
        if ($detail) {
            $address_info->detail = $detail;
        }
        if ($default) {
            $address_info->is_default = $default;
        }
        $address_info->lon=$lon;
        $address_info->lat=$lat;

        if ($default == 1) {
            AddressModel::update(['is_default' => 0],['uid' => $uid]);
        }
        $result = $address_info->save();
        if ($result) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }
    /**
     * 删除地址
     *
     * @param string $address_id 地址ID
     */
    public function del_address()
    {
        $uid = $this->auth->id;
        $address_id = $this->request->request('address_id');
        $address    = AddressModel::get(['id' => $address_id,'uid' => $uid]);
        if (!$address) {
            $this->error('地址错误');
        }
        $result = AddressModel::destroy($address_id);
        if ($result) {
            $default = AddressModel::where(['uid' => $uid])->order('id desc')->find();
            if ($default) {
                AddressModel::update(['is_default' => 1],['id' => $default->id]);
            }
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    /**
     * 设置默认地址
     *
     * @param string $address_id 地址ID
     */
    public function default()
    {
        $uid = $this->auth->id;

        $address_id = $this->request->request('address_id');
        $address    = AddressModel::get(['id' => $address_id,'uid' => $uid]);
        if (!$address) {
            $this->error('地址不存在');
        }
        if ($address->is_default != 0) {
            $this->error('已是默认地址');
        }
        AddressModel::update(['is_default' => 0],['uid' => $uid]);
        $result = AddressModel::update(['is_default' => 1],['id' => $address_id]);
        if ($result) {
            $this->success('设置成功');
        } else {
            $this->error('设置失败');
        }
    }

  /**
   * 获取默认地址
   */
  public function get_default()
  {
    $uid     = $this->auth->id;
    $address = \app\common\model\Address::get(['is_default' => 1,'uid' => $uid]);
    $this->success('请求成功',$address);
  }
}